package com.zhaokuner.parentstarter.handler;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * <p>
 * Description: req info
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author 赵希坤 zhaoxikun
 * @date 2021/12/19 6:51 下午
 */
public class HttpReqInfo {

    private HttpServletRequest request;

    private HttpServletResponse response;

    /**
     * 请求发起的服务ID
     */
    private String fromServiceId;
    /**
     * 请求发起的服务版本
     */
    private String fromServiceVersion;
    /**
     * 服务ID
     */
    private String serviceId;
    /**
     * 服务版本
     */
    private String serviceVersion;
    /**
     * 会话id
     */
    private String sessionId;
    /**
     * 语言
     */
    private String language;
    /**
     * 是否初始化可用
     */
    private AtomicBoolean usable;

    private HttpHeaders headers;


    public HttpReqInfo() {
        this.usable = new AtomicBoolean(false);
        this.headers = new HttpHeaders();
    }

    protected void init(HttpServletRequest request, HttpServletResponse response) {
        if (!usable.compareAndSet(false, true)) {
            //已经初始化过
            return;
        }
        this.request = request;
        this.response = response;
        {
            this.setServiceId(HttpReqInfoUtil.appServiceId);
            this.setServiceVersion(HttpReqInfoUtil.appServiceVersion);
            this.setLanguage(request.getHeader(HttpReqInfoUtil.LANGUAGE));
            this.setSessionId(request.getHeader(HttpReqInfoUtil.LENOVO_SESSION_ID));
            this.setFromServiceId(request.getHeader(HttpReqInfoUtil.SERVICE_ID));
            this.setFromServiceVersion(request.getHeader(HttpReqInfoUtil.SERVICE_VERSION));
        }
        {
            headers = new HttpHeaders();
            Enumeration<String> headerNames = request.getHeaderNames();
            if (null != headerNames) {
                while (headerNames.hasMoreElements()) {
                    String name = headerNames.nextElement();
                    headers.set(name, request.getHeader(name));
                }
            }
        }
        {
            //当前app的
            headers.set(HttpReqInfoUtil.SERVICE_ID, serviceId);
            headers.set(HttpReqInfoUtil.SERVICE_VERSION, serviceVersion);
            if (StringUtils.isNotBlank(language)) {
                headers.set(HttpReqInfoUtil.LANGUAGE, language);
            }
            if (StringUtils.isNotBlank(sessionId)) {
                headers.set(HttpReqInfoUtil.LENOVO_SESSION_ID, sessionId);
            }
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        }
    }


    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public String getServiceId() {
        return serviceId;
    }

    public void setServiceId(String serviceId) {
        this.serviceId = serviceId;
    }

    public String getServiceVersion() {
        return serviceVersion;
    }

    public void setServiceVersion(String serviceVersion) {
        this.serviceVersion = serviceVersion;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }


    public HttpHeaders getHeaders() {
        return headers;
    }

    public void setHeaders(HttpHeaders headers) {
        this.headers = headers;
    }

    public String getFromServiceId() {
        return fromServiceId;
    }

    public void setFromServiceId(String fromServiceId) {
        this.fromServiceId = fromServiceId;
    }

    public String getFromServiceVersion() {
        return fromServiceVersion;
    }

    public void setFromServiceVersion(String fromServiceVersion) {
        this.fromServiceVersion = fromServiceVersion;
    }

    @Override
    public String toString() {
        return "HttpReqInfo["
                + " request=" + request + ","
                + " response=" + response + ","
                + " serviceId=" + serviceId + ","
                + " serviceVersion=" + serviceVersion + ","
                + " sessionId=" + sessionId + ","
                + " language=" + language + ","
                + " usable=" + usable + ","
                + " headers=" + headers
                + " ]";
    }
}
